package com.lazyfish.scss.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.lazyfish.scss.bean.User;
import com.lazyfish.scss.dao.UserDAO;
import com.lazyfish.scss.util.ValidateJson;

/**
 * Servlet implementation class Register
 */
@WebServlet("/Login")
public class LoginServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public LoginServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        Map<String,String> map=new HashMap<String,String>();
        User user=new User();//用于登录的user
        User user2=new User();//获取seesion中的用户信息
        User user3=new User();//进行深度拷贝每，因为要去除密码
        user2=null;
        //判断是否已经登录，如果已经登录显示用户信息
        user2 = (User)request.getSession().getAttribute("user2");
        try {
            user3=(User)user2.clone();
        } catch (Exception e) {
            System.out.println("未登录");
            user3=null;
        }

        if(user3!=null){
            //把密码清空，将用户信息回传
            user3.setPassword("");
            PrintWriter out = response.getWriter();
            String ListUserJson = JSON.toJSONString(user3);
            out.write(ListUserJson);
            return;
        }
        JSONObject json;
        String username = null;
        String password = null;
        boolean flag=true;
        json = ValidateJson.verification(request, "account","password");
        if(json!=null){
            username = json.getString("account");
            password = json.getString("password");
        }else{
            map.put("status", "-1");
            map.put("msg", "缺少必要参数。");
            flag=false;
        }
        //参数齐全下进行登录认证
        if(flag){
            user.setAccount(username);
            user.setPassword(password);
            user2=new UserDAO().login_check(user);
            if( user2!=null && user2.getAccount()!=null){
                map.put("status", "200");
                map.put("msg", "欢迎您，登录成功~！");
                HttpSession s2 = request.getSession();
                s2.setMaxInactiveInterval(24*60*60);
                s2.setAttribute("user2",user2);
            }else{
                map.put("status", "-1");
                map.put("msg", "登录失败，可能是账号密码错误。");
            }
        }
        //输出json格式的map
        PrintWriter out = response.getWriter();
        String ListUserJson = JSON.toJSONString(map);
        out.write(ListUserJson);

    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }

}
